package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.LibraryBook;
import com.atguigu.campusintelligentassistantsystem.entity.StudentBook;
import com.atguigu.campusintelligentassistantsystem.service.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/library")
@CrossOrigin(origins = "*")
public class LibraryController {

    @Autowired
    private LibraryService libraryService;

    /**
     * 搜索图书
     * @param keyword 关键词
     * @return 图书列表
     */
    @GetMapping("/search")
    public ResponseEntity<List<LibraryBook>> searchBooks(@RequestParam String keyword) {
        List<LibraryBook> books = libraryService.searchBooks(keyword);
        return ResponseEntity.ok(books);
    }

    /**
     * 根据ISBN查找图书
     * @param isbn ISBN
     * @return 图书列表
     */
    @GetMapping("/isbn/{isbn}")
    public ResponseEntity<List<LibraryBook>> findBooksByIsbn(@PathVariable String isbn) {
        List<LibraryBook> books = libraryService.findBooksByIsbn(isbn);
        return ResponseEntity.ok(books);
    }

    /**
     * 获取所有图书
     * @return 图书列表
     */
    @GetMapping
    public ResponseEntity<List<LibraryBook>> getAllBooks() {
        List<LibraryBook> books = libraryService.getAllBooks();
        return ResponseEntity.ok(books);
    }

    /**
     * 根据ID获取图书
     * @param id 图书ID
     * @return 图书信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<LibraryBook> getBookById(@PathVariable Long id) {
        LibraryBook book = libraryService.getBookById(id);
        if (book != null) {
            return ResponseEntity.ok(book);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 添加新图书
     * @param book 图书信息
     * @return 保存后的图书
     */
    @PostMapping
    public ResponseEntity<LibraryBook> addBook(@RequestBody LibraryBook book) {
        LibraryBook savedBook = libraryService.addBook(book);
        return ResponseEntity.ok(savedBook);
    }

    /**
     * 更新图书信息
     * @param id 图书ID
     * @param book 图书信息
     * @return 更新后的图书
     */
    @PutMapping("/{id}")
    public ResponseEntity<LibraryBook> updateBook(@PathVariable Long id, @RequestBody LibraryBook book) {
        book.setId(id);
        LibraryBook updatedBook = libraryService.updateBook(book);
        return ResponseEntity.ok(updatedBook);
    }

    /**
     * 删除图书
     * @param id 图书ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteBook(@PathVariable Long id) {
        libraryService.deleteBook(id);
        return ResponseEntity.ok("图书删除成功");
    }

    /**
     * 学生借阅图书
     * @param studentId 学生ID
     * @param bookId 图书ID
     * @return 借阅记录
     */
    @PostMapping("/borrow")
    public ResponseEntity<StudentBook> borrowBook(@RequestParam Long studentId, @RequestParam Long bookId) {
        StudentBook studentBook = libraryService.borrowBook(studentId, bookId);
        if (studentBook != null) {
            return ResponseEntity.ok(studentBook);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 续借图书
     * @param id 借阅记录ID
     * @return 更新后的借阅记录
     */
    @PutMapping("/{id}/renew")
    public ResponseEntity<StudentBook> renewBook(@PathVariable Long id) {
        StudentBook studentBook = libraryService.renewBook(id);
        if (studentBook != null) {
            return ResponseEntity.ok(studentBook);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 获取学生的借阅记录
     * @param studentId 学生ID
     * @return 借阅记录列表
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<StudentBook>> getStudentBooks(@PathVariable Long studentId) {
        List<StudentBook> studentBooks = libraryService.getStudentBooks(studentId);
        return ResponseEntity.ok(studentBooks);
    }

    /**
     * 获取图书的借阅记录
     * @param bookId 图书ID
     * @return 借阅记录列表
     */
    @GetMapping("/book/{bookId}")
    public ResponseEntity<List<StudentBook>> getBookBorrowRecords(@PathVariable Long bookId) {
        List<StudentBook> bookBorrowRecords = libraryService.getBookBorrowRecords(bookId);
        return ResponseEntity.ok(bookBorrowRecords);
    }
}